package com.insurance.service.impl;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.mapper.UserKFMapper;
import com.insurance.mapper.UserMapper;
import com.insurance.pojo.Driver_evaluation;
import com.insurance.pojo.UserOrder;
import com.insurance.service.UserKFService;
import com.insurance.service.UserServcie;
import com.insurance.utils.ConversionDateUtils;
import com.insurance.utils.PasswordEncryption;
import com.insurance.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/30 14:58
 * Description:
 * Version: V1.0
 */
@Service
public class UserKFServiceImpl implements UserKFService{

    @Autowired(required = false)
    private UserKFMapper userKFMapper;

    @Override
    public Map queryUserPwdByUserId(int user_id) {
        //查询当前id对应的用户
        Map map = userKFMapper.queryUserPwdByUserId(user_id);
        return map;
    }

    @Override
    public int updateUserPwdByUserId(int user_id, String user_pwd) {
        //密码加密
        String new_pwd = PasswordEncryption.passwordEncryption(user_pwd);
        int result = userKFMapper.updateUserPwdByUserId(user_id,new_pwd);
        return result;
    }

    @Override
    public List<Map> queryUserCouponByUserId(int user_id) {
        List<Map> mapList = userKFMapper.queryUserCouponByUserId(user_id);
        for (Map map:mapList) {
            String start_date = ConversionDateUtils.conversionDateStringUtils(map.get("start_time"));
            map.put("start_time",start_date);
        }
        for (Map map:mapList) {
            String end_date = ConversionDateUtils.conversionDateStringUtils(map.get("end_time"));
            map.put("end_time",end_date);
        }
        return mapList;
    }

    @Override
    public List<Map> queryRepairByAreaId(int area_city_id) {
        return userKFMapper.queryRepairByAreaId(area_city_id);
    }

    @Override
    public List<Map> query4SByAreaId(int area_city_id) {
        return userKFMapper.query4SByAreaId(area_city_id);
    }

    @Override
    public List<Map> queryDriverByAreaId(int area_city_id) {
        return userKFMapper.queryDriverByAreaId(area_city_id);
    }

    @Override
    public List<Map> queryInsuranceByUserId(int user_id) {
        return userKFMapper.queryInsuranceByUserId(user_id);
    }

    @Override
    public ResultData queryUserOrderAllUnreadByUserId(int user_id) {
        ResultData resultData=new ResultData();
        /**
         * 查询订单信息
         */
        List<Map> list=userKFMapper.queryOrder(user_id);
        /**
         * 查询图片附件地址
         */
        for (Map map:list){
            if (map.get("car_picktime")!=null){
                map.put("car_picktime",ConversionDateUtils.conversionDateStringUtils(map.get("car_picktime")));

            }
            if (map.get("order_time")!=null){
                map.put("order_time",ConversionDateUtils.conversionDateStringUtils(map.get("order_time")));

            }
            if (map.get("expectfinish_time")!=null){
            map.put("expectfinish_time",ConversionDateUtils.conversionDateStringUtils(map.get("expectfinish_time")));
            }
            String[] strings=  userKFMapper.queryOrderImg(map.get("order_number"));
            String[] strings2= userKFMapper.queryOrderRegImg(map.get("order_number"));;
            map.put("repair_img_address",strings);
            map.put("img_address",strings2);
        }
        if (list.size()>0){
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }
    @Override
    public ResultData addOrder(UserOrder userOrder) {
        ResultData resultData=new ResultData();
        userOrder.setOrderNumber(UuidUtil.getUUID());
        int res=userKFMapper.addOrder(userOrder);
        int order_id=userOrder.getOrder_id();
        //添加图片附件
        int resImg=userKFMapper.addImgAddress(order_id,userOrder.getImg_address());
        //添加修理部位图片地址
        int regImg=userKFMapper.addReImgAddress(order_id,userOrder.getRepair_img_address());
        //查询类型的金额
        Double price=userKFMapper.querOrderTypePrice(userOrder.getOrderType());
        //插入到支付表中
        String pay_number=UuidUtil.getUUID();
        int pay=userKFMapper.addPayInfo(pay_number,userOrder.getOrderNumber(),price);
        Map map=userKFMapper.queryPayOrderByOrderNumber(userOrder.getOrderNumber());
        if (pay>0){
            resultData.setData(map);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("添加失败");
        }
        return resultData;
    }

    @Override
    public ResultData queryOwnerCardByUserId(int user_id) {
        ResultData resultData=new ResultData();
        List<Map> list=userKFMapper.queryOwnerCardByUserId(user_id);
        if (list.size()>0){
            for (Map map:list) {
                map.put("owner_first_date",ConversionDateUtils.conversionDateStringUtils(map.get("owner_first_date")));
            }
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setMsg("查无数据");
            resultData.setCode(Contants.FAILURE);
        }
        return resultData;
    }
}
